package cn.tedu.sims.service.impl;

import cn.tedu.sims.ex.ServiceException;
import cn.tedu.sims.mapper.RoleMapper;
import cn.tedu.sims.pojo.dto.RoleDTO;
import cn.tedu.sims.pojo.dto.RolePowerUpdateDTO;
import cn.tedu.sims.pojo.dto.RoleUpdateDTO;
import cn.tedu.sims.pojo.entity.Power;
import cn.tedu.sims.pojo.entity.Role;
import cn.tedu.sims.pojo.entity.User;
import cn.tedu.sims.service.IRoleService;
import cn.tedu.sims.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void addNew(RoleDTO roleDTO){
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO,role);
        int rows = roleMapper.insert(role);
        if(rows != 1){
            String message = "角色插入失败,服务器繁忙!!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id){
        int rows = roleMapper.deleteById(id);
        if(rows != 1){
            String message = "角色删除失败,服务器繁忙!!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        //删除要将用户相关删除
    }

    @Override
    public List<Role> list(){
        return roleMapper.list();
    }

    @Override
    public List<Role> selectByWd(String wd){
        return roleMapper.selectByWd(wd);
    }

    @Override
    public Role getStandardById(Long id) {
        return roleMapper.getStandardById(id);
    }

    @Override
    public void update(RoleUpdateDTO roleUpdateDTO) {
        String roleName = roleUpdateDTO.getName();
        int count = roleMapper.countByName(roleName);
        if(count > 0){
            String message = "角色名称已被占用,修改失败!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleUpdateDTO,role);
        int rows = roleMapper.update(role);
        if(rows != 1){
            String message = "修改角色失败，服务器忙，请稍后再尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public List<User> selectByRoleId(Long id) {
        return roleMapper.selectByRoleId(id);
    }

    @Override
    public List<Power> selectPower() {
        return roleMapper.selectPower();
    }

    @Override
    public List<Power> selectPowerBySon(Long id) {
        return roleMapper.selectPowerBySon(id);
    }

    @Override
    public List<Long> selectPowerExist(Long id) {
        return roleMapper.selectPowerExist(id);
    }

    @Override
    public void updateRolePower(RolePowerUpdateDTO r) {
        if (r.getPowers()==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"修改失败,权限不能为空");
        }

        int countById = roleMapper.countById(r.getRoleId());
        if (countById!=1){
         throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"修改失败,角色不存在");
        }

        int countByPower = roleMapper.countByPower(r.getPowers());
        if (countByPower!=r.getPowers().size()){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"修改失败,权限不存在");
        }


        int deleteByRolePower = roleMapper.deleteByRolePower(r.getRoleId());


        int sum=0;
        for (Long powerId: r.getPowers()){
            System.out.println("powerId:"+powerId+",RoleId:"+r.getRoleId());
            roleMapper.insertRolePower(powerId,r.getRoleId());
            sum+=1;

        }
        if (sum!=r.getPowers().size()){
            throw new ServiceException(ServiceCode.ERR_INSERT,"修改失败,服务器繁忙");
        }

    }
}
